Passed
Push — development ( e131c7...d0a4d7 )
by Peter
05:27 queued 13s
created

Map.tsx ➔ Map   B

Complexity

Conditions 5

Size

Total Lines 84
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 71
dl 0
loc 84
rs 7.4824
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { MapContainer, Popup, Marker, TileLayer, Polygon, Tooltip} from 'react-leaflet';
2
import { useEffect, useState, useRef } from 'react';
3
import { LatLngTuple,  LatLngExpression } from 'leaflet';
4
import { API_URL, getHeader, iconStation } from '../helpers/config';
5
import axios from 'axios';
6
import { RootState } from '../redux/store/store';
7
import { useDispatch, useSelector } from 'react-redux';
8
import { Scooter, PolygonPoint, SpeedZone, Zone } from '../helpers/map/leaflet-types'
9
import { useParams } from "react-router-dom";
10
import { cities } from '../helpers/map/cities';
11
import MapCenter from './MapCenter';
12
import { renderScooterMarkers, renderStationMarkers, renderPolygons } from '../helpers/map/renders';
13
14
export default function Map() {
15
    const { city }  = useParams();
16
    const [startPosition, setStartPosition] = useState<LatLngExpression>([59.2741, 15.2066]);
17
    const {isLoggedIn, token, user, role} = useSelector((state: RootState) =>  state.auth);
18
    const [scooterData, setScooterData] = useState<Scooter[]>([]);
19
    const [zoneData, setZoneData] = useState<Zone[]>([]);
20
    const zoom = 11;
21
    const stationPositions: LatLngTuple[] = [[51.505, -0.04],[51.515, -0.15],[51.535, -0.08]];
22
23
    useEffect(() => {
24
        if (city && cities[city]) {
25
            setStartPosition(cities[city]);
26
        }
27
    }, [city]);
28
    
29
30
    useEffect(() => {
31
        const fetchScooters = async() => {
32
        try {
33
                const response = await axios.get(`${API_URL}/bike/city/${city}`);
34
                console.log(response.data)
35
                setScooterData(response.data);
36
            }
37
            catch(error)
38
            {
39
            }
40
      }
41
      fetchScooters();
42
      },[])
43
    
44
      useEffect(() => {
45
        const fetchZones = async() => {
46
        try {
47
48
                const response = await axios.get(`${API_URL}/zone/city/${city}`);
49
                setZoneData(response.data);
50
            }
51
            catch(error)
52
            {
53
            }
54
      }
55
      fetchZones();
56
      },[])
57
58
    
59
  return (
60
    <div id="map-container">
61
            <div id="map" data-testid="map">
62
                <MapContainer
63
                    style={{ height: "400px" }}
64
                    center={startPosition}
65
                    zoom={zoom}
66
                    scrollWheelZoom={true}
67
                >
68
                    <TileLayer
69
                        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
70
                        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
71
                    />
72
                    <MapCenter center={startPosition} zoom={zoom} />
73
                    {renderScooterMarkers(scooterData)}
74
                    {renderStationMarkers(stationPositions)}
75
                    {renderPolygons(zoneData)}
76
                </MapContainer>
77
            </div>
78
            <div id="scooter-list" className="mt-4 bg-gray-600 rounded">
79
                <h2 className="text-xl font-bold mb-2">Cyklar i {city}:</h2>
80
                {scooterData.length > 0 ? (
81
                    <ul className="list-disc pl-6 list-none">
82
                        {scooterData.map((scooter) => (
83
                            <li key={scooter.id} className="mb-2">
84
                                <div className="mt-4 p-6 mx-auto w-1/2 hover:opacity-5 bg-gray-400 rounded text-center">
85
                                <h2><span className="font-semibold">ID:</span>{scooter.id} -{" "}</h2>
86
                                <span className="font-semibold">Batteri:</span> {scooter.batteryLevel}% -{" "}
87
                                <span className="font-semibold">Status:</span> {scooter.status}
88
                                </div>
89
                            </li>
90
                        ))}
91
                    </ul>
92
                ) : (
93
                    <p>Inga cyklar tillgängliga i denna stad.</p>
94
                )}
95
            </div>
96
        </div>
97
  )
98
};
99